﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;

public class TextureFormatTool
{
    // 配置路径
    // readonly static string ETC_SHADER_PATH = "Assets/Resources/Shader/EtcAlpha_1.shader";   // etc1 的配置路径  但是etc1已经差不多被etc2取代了，所以使用etc2是完全没有问题的。
    readonly static string ATLER_FORMAT_CONFIG_PATH = Application.dataPath + "/Editor/ProcessTexture/TextureFormatConfig.txt";


    #region 对图片纹理进行格式化__已经废弃 

    ///// <summary>
    ///// 格式化Texture
    ///// NGUI调用入口、生成图片入口
    ///// </summary>
    ///// <param name="tex_path">贴图路径</param>
    ///// <param name="mat_path">材质球路径</param>
    //public static void FormatTexture(string tex_path, string mat_path)
    //{
    //    if (string.IsNullOrEmpty(tex_path) || !System.IO.File.Exists(tex_path))
    //    {
    //        Debug.Log("Can't find tex_path path: " + tex_path);
    //        return;
    //    }
    //    if (string.IsNullOrEmpty(mat_path) || !System.IO.File.Exists(mat_path))
    //    {
    //        Debug.Log("Can't find mat_path path: " + mat_path);
    //        return;
    //    }

    //    // 临时存储通道路径
    //    string alpha_tex_path = "";

    //    // 读取配置表，获取压缩格式
    //    // 根据压缩格式调用对应的压缩代码压缩图片
    //    // 压缩图片
    //    var tex_name = System.IO.Path.GetFileNameWithoutExtension(tex_path);
    //    var _tex_path = tex_path.Replace("\\", "/");
    //    var asset_path = Application.dataPath.Replace("Assets", "").Replace("\\", "/");
    //    asset_path = _tex_path.Replace(asset_path, "");
    //    var tex_format = GetTextureFormatFromConfig(asset_path);

    //    // 对于rpga16 进行特殊处理。抖动
    //    // 对于etc进行剥离通道
    //    if (tex_format == TextureImporterFormat.RGBA16)
    //    {
    //        SetTextureDither(tex_path);
    //    }
    //    else if (tex_format == TextureImporterFormat.ETC_RGB4)
    //    {
    //        SetTextureFormat(tex_path, TextureImporterFormat.RGBA32);
    //        alpha_tex_path = FormatTextureToRGB4(tex_path);
    //        SetTextureFormat(alpha_tex_path, TextureImporterFormat.ETC_RGB4);
    //    }

    //    // 设置材质球的shader
    //    if ((tex_format == TextureImporterFormat.ETC_RGB4))
    //    {
    //        Texture tex_rgb = AssetDatabase.LoadAssetAtPath(tex_path, typeof(Texture)) as Texture;
    //        Texture tex_alpha = AssetDatabase.LoadAssetAtPath(alpha_tex_path, typeof(Texture)) as Texture;

    //        Material mat = AssetDatabase.LoadAssetAtPath(mat_path, typeof(Material)) as Material;
    //        Shader shader = AssetDatabase.LoadAssetAtPath(ETC_SHADER_PATH, typeof(Shader)) as Shader;
    //        mat.shader = shader;
    //        mat.SetTexture("_MainTex", tex_rgb);
    //        mat.SetTexture("_MainTex_A", tex_alpha);
    //    }
    //    else
    //    {
    //        Texture tex_rgb = AssetDatabase.LoadAssetAtPath(tex_path, typeof(Texture)) as Texture;
    //        Material mat = AssetDatabase.LoadAssetAtPath(mat_path, typeof(Material)) as Material;
    //        Shader shader = Shader.Find(NGUISettings.atlasPMA ? "Unlit/Premultiplied Colored" : "Unlit/Transparent Colored");
    //        mat.shader = shader;
    //        mat.SetTexture("_MainTex", tex_rgb);
    //    }

    //    SetTextureFormat(tex_path, tex_format);
    //}

    ///// <summary>
    ///// 尝试剥离通道
    ///// </summary>
    //public static bool TryStripAlphaTexture(string png_file_path)
    //{
    //    // 查看该贴图是否需要进行etc处理
    //    // 读取.png的设置
    //    // 格式化.png纹理为rgb24
    //    // 剥离通道
    //    // 还原.png 贴图纹理
    //    var file_name = Path.GetFileNameWithoutExtension(png_file_path);
    //    var _png_path = png_file_path.Replace("\\", "/");
    //    var asset_path = Application.dataPath.Replace("Assets", "").Replace("\\", "/");
    //    asset_path = _png_path.Replace(asset_path, "");
    //    var format = GetTextureFormatFromConfig(asset_path);
    //    if (format != TextureImporterFormat.ETC_RGB4)
    //    {
    //        Debug.Log("This texture can not convert format to ETC_RGB4 ! please check config. ");
    //        return false;
    //    }

    //    SetTextureFormat(png_file_path, TextureImporterFormat.RGBA32);
    //    TextureImporterFormat tex_format = GetTextureFormatFromConfig(asset_path);
    //    AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);

    //    var alpha_tex_path = FormatTextureToRGB4(png_file_path);
    //    Debug.Log("BuildAlphaTexture:" + png_file_path + "  alpha_tex_path:" + alpha_tex_path);
    //    SetTextureFormat(alpha_tex_path, TextureImporterFormat.ETC_RGB4);

    //    SetTextureFormat(png_file_path, tex_format);

    //    return true;
    //}


//    /// <summary>
//    /// 格式化图片 修改texture的meta文件
//    /// 直接设置双平台纹理格式
//    /// 当纹理格式为 ETC_RGB4 时会将iphone设置为pv4
//    /// </summary>
//    private static void SetTextureFormat(string texture_path, TextureImporterFormat format)
//    {
//        // 进行贴图的统一的优化设置
//        // 再设置纹理格式
//        TextureImporter tex_importer = GetTextureImporter(texture_path);
//        tex_importer.textureType = TextureImporterType.Default;
//        tex_importer.mipmapEnabled = false;
//        tex_importer.isReadable = false;
//        tex_importer.filterMode = FilterMode.Bilinear;
//#if UNITY_5_6_3
//        var iphone_set_info = new TextureImporterPlatformSettings();
//        iphone_set_info.name = "iPhone";
//        iphone_set_info.maxTextureSize = tex_importer.maxTextureSize;
//        iphone_set_info.format = format;
//        iphone_set_info.allowsAlphaSplitting = format != TextureImporterFormat.ETC_RGB4;
//        tex_importer.SetPlatformTextureSettings(iphone_set_info);

//        var android_set_info = new TextureImporterPlatformSettings();
//        android_set_info.name = "Android";
//        android_set_info.maxTextureSize = tex_importer.maxTextureSize;
//        android_set_info.format = format;
//        tex_importer.SetPlatformTextureSettings(android_set_info);
//        iphone_set_info.allowsAlphaSplitting = format != TextureImporterFormat.ETC_RGB4;
//        tex_importer.SetPlatformTextureSettings(android_set_info);
//#else
//        tex_importer.SetPlatformTextureSettings("iPhone", tex_importer.maxTextureSize, format, format != TextureImporterFormat.ETC_RGB4);
//        tex_importer.SetPlatformTextureSettings("Android", tex_importer.maxTextureSize, format, format != TextureImporterFormat.ETC_RGB4);
//#endif
//        tex_importer.SaveAndReimport();
//    }

    ///// <summary>
    ///// Floyd–Steinberg dithering
    ///// 抖动处理，需要给图片进行抖动处理
    ///// 需要注意的是 需要进行抖动处理的是RGBA32的纹理进行抖动处理，再压缩为16位
    ///// </summary>
    //private static void SetTextureDither(string tex_path)
    //{
    //    SetTextureReadable(tex_path, true);
    //    var texture = AssetDatabase.LoadAssetAtPath(tex_path, typeof(Texture2D)) as Texture2D;
    //    var texw = texture.width;
    //    var texh = texture.height;

    //    var pixels = texture.GetPixels();
    //    var offs = 0;

    //    var k1Per15 = 1.0f / 15.0f;
    //    var k1Per16 = 1.0f / 16.0f;
    //    var k3Per16 = 3.0f / 16.0f;
    //    var k5Per16 = 5.0f / 16.0f;
    //    var k7Per16 = 7.0f / 16.0f;

    //    for (var y = 0; y < texh; y++)
    //    {
    //        for (var x = 0; x < texw; x++)
    //        {
    //            float a = pixels[offs].a;
    //            float r = pixels[offs].r;
    //            float g = pixels[offs].g;
    //            float b = pixels[offs].b;

    //            var a2 = Mathf.Clamp01(Mathf.Floor(a * 16) * k1Per15);
    //            var r2 = Mathf.Clamp01(Mathf.Floor(r * 16) * k1Per15);
    //            var g2 = Mathf.Clamp01(Mathf.Floor(g * 16) * k1Per15);
    //            var b2 = Mathf.Clamp01(Mathf.Floor(b * 16) * k1Per15);

    //            var ae = a - a2;
    //            var re = r - r2;
    //            var ge = g - g2;
    //            var be = b - b2;

    //            pixels[offs].a = a2;
    //            pixels[offs].r = r2;
    //            pixels[offs].g = g2;
    //            pixels[offs].b = b2;

    //            var n1 = offs + 1;   // (x+1,y)
    //            var n2 = offs + texw - 1; // (x-1 , y+1)
    //            var n3 = offs + texw;  // (x, y+1)
    //            var n4 = offs + texw + 1; // (x+1 , y+1)

    //            if (x < texw - 1)
    //            {
    //                pixels[n1].a += ae * k7Per16;
    //                pixels[n1].r += re * k7Per16;
    //                pixels[n1].g += ge * k7Per16;
    //                pixels[n1].b += be * k7Per16;
    //            }

    //            if (y < texh - 1)
    //            {
    //                pixels[n3].a += ae * k5Per16;
    //                pixels[n3].r += re * k5Per16;
    //                pixels[n3].g += ge * k5Per16;
    //                pixels[n3].b += be * k5Per16;

    //                if (x > 0)
    //                {
    //                    pixels[n2].a += ae * k3Per16;
    //                    pixels[n2].r += re * k3Per16;
    //                    pixels[n2].g += ge * k3Per16;
    //                    pixels[n2].b += be * k3Per16;
    //                }

    //                if (x < texw - 1)
    //                {
    //                    pixels[n4].a += ae * k1Per16;
    //                    pixels[n4].r += re * k1Per16;
    //                    pixels[n4].g += ge * k1Per16;
    //                    pixels[n4].b += be * k1Per16;
    //                }
    //            }

    //            offs++;
    //        }
    //    }
    //    SetTextureReadable(tex_path, false);
    //    SaveTexture(tex_path, texture, pixels);
    //}


//    public static void ConvertTextureFormatToTargetFormat(string tex_path, TextureImporterFormat target_format)
//    {
//        var file_name = Path.GetFileNameWithoutExtension(tex_path);
//        SetTextureReadable(tex_path, true);
//        Texture2D tex = AssetDatabase.LoadAssetAtPath(tex_path, typeof(Texture2D)) as Texture2D;
//        if (tex.format != UnityEngine.TextureFormat.RGBA32)
//        {
//            TextureImporter tex_importer = GetTextureImporter(tex_path);
//            tex_importer.textureType = TextureImporterType.Default;
//            tex_importer.mipmapEnabled = false;
//            tex_importer.isReadable = false;
//            tex_importer.filterMode = FilterMode.Bilinear;
//#if UNITY_5_6_3
//            var iphone_set_info = new TextureImporterPlatformSettings();
//            iphone_set_info.name = "iPhone";
//            iphone_set_info.maxTextureSize = tex_importer.maxTextureSize;
//            iphone_set_info.format = target_format;
//            tex_importer.SetPlatformTextureSettings(iphone_set_info);

//            var android_set_info = new TextureImporterPlatformSettings();
//            android_set_info.name = "Android";
//            android_set_info.maxTextureSize = tex_importer.maxTextureSize;
//            android_set_info.format = target_format;
//            tex_importer.SetPlatformTextureSettings(android_set_info);
//#else
//            tex_importer.SetPlatformTextureSettings("iPhone", tex_importer.maxTextureSize, target_format);
//            tex_importer.SetPlatformTextureSettings("Android", tex_importer.maxTextureSize, target_format);
//#endif
//            tex_importer.SaveAndReimport();
//        }
//        SetTextureReadable(tex_path, false);
//        AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
//    }

    ///// <summary>
    ///// ETC1的特殊处理，分离透明通道并且更改材质为双贴图
    ///// 能剥离通道的图片纹理格式只有 rgb24 或者 rgba32
    ///// </summary>
    //private static string FormatTextureToRGB4(string tex_path)
    //{
    //    // 生成透明通道的png文件
    //    // 对通道图片进行格式纹理设置
    //    // 剥离通道并存储
    //    var alpha_tex_path = tex_path.Replace(".png", "_alpha.png");
    //    ChangeTextureToAlphaTexture(tex_path, alpha_tex_path);
    //    return alpha_tex_path;
    //}

    ///// <summary>
    ///// 从RGBA32剥离alpha通道
    ///// </summary>
    //private static void ChangeTextureToAlphaTexture(string tex_path, string alpha_tex_path)
    //{
    //    SetTextureReadable(tex_path, true);
    //    Texture2D sourcetex = AssetDatabase.LoadAssetAtPath(tex_path, typeof(Texture2D)) as Texture2D;
    //    Color[] colors = sourcetex.GetPixels();
    //    SetTextureReadable(tex_path, false);


    //    // 格式化通道
    //    Texture2D tex_alpha = new Texture2D(sourcetex.width, sourcetex.height, UnityEngine.TextureFormat.RGB24, false);
    //    Color[] alpha_colors = new Color[colors.Length];
    //    for (int i = 0; i < colors.Length; ++i)
    //    {
    //        alpha_colors[i].r = colors[i].a;
    //        alpha_colors[i].g = colors[i].a;
    //        alpha_colors[i].b = colors[i].a;
    //    }

    //    SaveTexture(alpha_tex_path, tex_alpha, alpha_colors);
    //}

    //private static TextureImporter GetTextureImporter(string path)
    //{
    //    Debug.Log("path: " + path);
    //    TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
    //    textureImporter.textureType = TextureImporterType.GUI;
    //    textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
    //    return textureImporter;
    //}

    ///// <summary>  
    ///// 设置图片为可读格式  
    ///// </summary>  
    //private static void SetTextureReadable(string alpha_tex_path, bool is_allow_read)
    //{
    //    TextureImporter ti = (TextureImporter)TextureImporter.GetAtPath(alpha_tex_path);
    //    ti.isReadable = is_allow_read;
    //    ti.SaveAndReimport();
    //    //AssetDatabase.ImportAsset(alpha_tex_path);
    //}

    //private static void SaveTexture(string tex_path, Texture2D texture, Color[] pixels)
    //{
    //    SetTextureReadable(tex_path, true);
    //    texture.SetPixels(pixels);
    //    texture.Apply();
    //    var bytes = texture.EncodeToPNG();
    //    SetTextureReadable(tex_path, false);


    //    if (File.Exists(tex_path)) AssetDatabase.DeleteAsset(tex_path);
    //    var stream = File.Create(tex_path);
    //    stream.Write(bytes, 0, bytes.Length);
    //    stream.Close();

    //    AssetDatabase.ImportAsset(tex_path);
    //}

    //public static void DitherTexture(string png_file_path)
    //{
    //    var file_name = Path.GetFileNameWithoutExtension(png_file_path);
    //    var _png_path = png_file_path.Replace("\\", "/");
    //    var asset_path = Application.dataPath.Replace("Assets", "").Replace("\\", "/");
    //    asset_path = _png_path.Replace(asset_path, "");
    //    var format = GetTextureFormatFromConfig(asset_path);
    //    if (format == TextureImporterFormat.RGBA16)
    //    {
    //        SetTextureDither(png_file_path);
    //    }
    //    else
    //    {
    //        Debug.Log("can not diter texture ! check config! path: " + png_file_path);
    //    }
    //}

#endregion


    #region 读取配置表获取打包图集信息
    /// <summary>
    /// 获取打包图集的图片的大小
    /// </summary>
    public static int GetTextrueSize(string atler_name)
    {
        //var tex_name = System.IO.Path.GetFileNameWithoutExtension(tex_path);

        // 根据配置文件TextureFormat里面的配置寻找图集名称
        // 找到相关项就转换成相关的项的主键并且转换成枚举
        // 如果找不到就直接返回默认类型
        string tex_config_json_str = System.IO.File.ReadAllText(ATLER_FORMAT_CONFIG_PATH, System.Text.Encoding.UTF8);
        JavaScriptObject jsonObj = JavaScriptConvert.DeserializeObject<JavaScriptObject>(tex_config_json_str);

        TextureImporterFormat format = TextureImporterFormat.ETC_RGB4;
        if (jsonObj.ContainsKey("TextureSize"))
        {
            var item = (Dictionary<string, object>)jsonObj["TextureSize"];

            foreach (var node in item)
            {
                var key = node.Key;
                if (key.CompareTo("DefaultSize") != 0)
                {
                    JavaScriptArray list = (JavaScriptArray)node.Value;
                    foreach (string list_item in list)
                    {
                        if (list_item.CompareTo(atler_name) == 0)
                        {
                            return int.Parse(node.Key);
                        }
                    }
                }
            }
            if (item.ContainsKey("DefaultFarmat"))
            {
                return int.Parse((string)item["DefaultFarmat"]);
            }
        }
        else
        {
            Debug.LogError("Can't find config. config_path: " + ATLER_FORMAT_CONFIG_PATH);
        }

        return 1024;
    }

    /// <summary>
    /// 读取配置表获取贴图要采用什么格式格式化
    /// </summary>
    public static TextureImporterFormat GetTextureFormatFromConfig(string asset_tex_path)
    {
        // 根据配置文件TextureFormat里面的配置寻找图集名称
        // 找到相关项就转换成相关的项的主键并且转换成枚举
        // 如果找不到就直接返回默认类型
        string tex_config_json_str = System.IO.File.ReadAllText(ATLER_FORMAT_CONFIG_PATH, System.Text.Encoding.UTF8);
        JavaScriptObject jsonObj = JavaScriptConvert.DeserializeObject<JavaScriptObject>(tex_config_json_str);

        TextureImporterFormat format = TextureImporterFormat.RGBA32;
        if (jsonObj.ContainsKey("TextureFormat"))
        {
            var item = (Dictionary<string, object>)jsonObj["TextureFormat"];

            foreach (var node in item)
            {
                var key = node.Key;
                if (key.CompareTo("JPGDefaultFarmat") != 0 && key.CompareTo("PNGDefaultFarmat") != 0)
                {
                    JavaScriptArray list = (JavaScriptArray)node.Value;
                    foreach (string list_item in list)
                    {
                        if (list_item.CompareTo(asset_tex_path) == 0)
                        {
                            return (TextureImporterFormat)System.Enum.Parse(typeof(TextureImporterFormat), (string)(node.Key));
                        }
                    }
                }
            }

            var path = asset_tex_path.ToLower();
            if (path.EndsWith(".png") && item.ContainsKey("PNGDefaultFarmat"))
            {
                return (TextureImporterFormat)System.Enum.Parse(typeof(TextureImporterFormat), (string)(item["PNGDefaultFarmat"]));
            }
            else if (path.EndsWith(".jpg") && item.ContainsKey("JPGDefaultFarmat"))
            {
                return (TextureImporterFormat)System.Enum.Parse(typeof(TextureImporterFormat), (string)(item["JPGDefaultFarmat"]));
            }
            else
            {
                Debug.LogError("非工程指定图片类型,不进行设置..  path： " + asset_tex_path);
            }
        }
        else
        {
            Debug.LogError("Can't find config. config_path: " + ATLER_FORMAT_CONFIG_PATH);
        }
        return format;
    }
    #endregion


    #region 工具栏__已经废弃

    ///// <summary>
    ///// 剥离通道
    ///// </summary>
    //[MenuItem("TextureFormat/BuildAlphaTexture")]
    //public static void BuildAlphaTexture()
    //{
    //    GetSelectionPath(".png", (string file_path) =>
    //    {
    //        TryStripAlphaTexture(file_path);
    //    });
    //}

    ///// <summary>
    ///// 抖动处理图片
    ///// </summary>
    //[MenuItem("TextureFormat/DiterTexture")]
    //public static void DitherTexture()
    //{
    //    GetSelectionPath(".png", DitherTexture);
    //}

    ///// <summary>
    ///// 获取选择的文件进行操作
    ///// </summary>
    ///// <param name="match_end_with_str">结束匹配字段</param>
    ///// <param name="func">找到要找到的字段后执行的方法</param>
    //public static void GetSelectionPath(string match_end_with_str, Func func)
    //{
    //    if (func == null)
    //    {
    //        Debug.Log("no func");
    //        return;
    //    }

    //    if (!string.IsNullOrEmpty(match_end_with_str))
    //    {
    //        var objs = UnityEditor.Selection.objects;
    //        if (objs != null && objs.Length > 0)
    //        {
    //            for (int i = 0; i < objs.Length; ++i)
    //            {
    //                var file_path = AssetDatabase.GetAssetPath(objs[i]);
    //                if (file_path.EndsWith(match_end_with_str)) func(file_path);
    //            }
    //        }
    //    }
    //}

    //public delegate void Func(string selection_path);
    #endregion
}
